﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Reflection;
    using System.Resources;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.General;

    public class CustomColorDlg : Form
    {
        private NumericTextBox blueEdit;
        private Button button1;
        private Button button2;
        private Container components = null;
        private Point crossPos = new Point(0, 0);
        private Color currentColor = Color.White;
        private Panel currentColorPanel;
        private float DELTA_HEIGHT = 1.290323f;
        private float DELTA_WIDTH = 1.2f;
        private bool drawCrossHair = true;
        private NumericTextBox greenEdit;
        private NumericTextBox hueEdit;
        private Label Label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private Label label6;
        private Label label7;
        private PictureBox lumBox;
        private NumericTextBox lumEdit;
        private PictureBox paletteBox;
        private Color paletteColor;
        private NumericTextBox redEdit;
        private ResourceManager rm = null;
        private NumericTextBox satEdit;
        private bool updatingUI = false;

        public CustomColorDlg()
        {
            Assembly assembly = Assembly.GetAssembly(System.Type.GetType("YidanSoft.Library.EditorUtility.WinControls.CustomColorDlg"));
            this.rm = new ResourceManager("YidanSoft.Library.EditorUtility.Resources.ImagesColorPicker", assembly);
            this.InitializeComponent();
            this.paletteBox.Image = (Bitmap) this.rm.GetObject("Palette");
        }

        private void blueEdit_TextChanged(object sender, EventArgs e)
        {
            if (!this.updatingUI)
            {
                this.RGBTextChanged();
            }
        }

        private void CalculatePaletteMarkerCoordinates(int Hue, int Sat)
        {
            this.crossPos.X = (int) (((float) Hue) / this.DELTA_WIDTH);
            this.crossPos.Y = (int) (((float) (240 - Sat)) / this.DELTA_HEIGHT);
            this.paletteBox.Invalidate();
        }

        private void ConvertToRGB(float h, float s, float l)
        {
            float r = 0f;
            float g = 0f;
            float b = 0f;
            ColorUtil.HSLToRGB(h, s, l, ref r, ref g, ref b);
            this.CurrentColor = Color.FromArgb((int) r, (int) g, (int) b);
            this.redEdit.Text = Convert.ToInt32(r).ToString();
            this.greenEdit.Text = Convert.ToInt32(g).ToString();
            this.blueEdit.Text = Convert.ToInt32(b).ToString();
            this.currentColorPanel.BackColor = this.CurrentColor;
            ColorUtil.HSLToRGB(h, s, 120f, ref r, ref g, ref b);
            this.paletteColor = Color.FromArgb((int) r, (int) g, (int) b);
            this.lumBox.Invalidate();
        }

        private void CustomColorDlg_Load(object sender, EventArgs e)
        {
            this.updatingUI = true;
            this.currentColorPanel.BackColor = this.CurrentColor;
            float r = this.CurrentColor.R;
            float g = this.CurrentColor.G;
            float b = this.CurrentColor.B;
            this.redEdit.Text = Convert.ToInt32(r).ToString();
            this.greenEdit.Text = Convert.ToInt32(g).ToString();
            this.blueEdit.Text = Convert.ToInt32(b).ToString();
            float h = 0f;
            float s = 0f;
            float l = 0f;
            ColorUtil.RGBToHSL((int) r, (int) g, (int) b, ref h, ref s, ref l);
            this.hueEdit.Text = Convert.ToString((int) h);
            this.satEdit.Text = Convert.ToString((int) s);
            this.lumEdit.Text = Convert.ToString((int) l);
            ColorUtil.HSLToRGB(h, s, 120f, ref r, ref g, ref b);
            this.paletteColor = Color.FromArgb((int) r, (int) g, (int) b);
            this.lumBox.Invalidate();
            this.CalculatePaletteMarkerCoordinates((int) h, (int) s);
            this.updatingUI = false;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void greenEdit_TextChanged(object sender, EventArgs e)
        {
            if (!this.updatingUI)
            {
                this.RGBTextChanged();
            }
        }

        private void hueEdit_TextChanged(object sender, EventArgs e)
        {
            if (!this.updatingUI)
            {
                this.HueSatTextBoxChanged();
            }
        }

        private void HueSatChanged()
        {
            this.updatingUI = true;
            float h = this.crossPos.X * this.DELTA_WIDTH;
            float s = 240f - (this.crossPos.Y * this.DELTA_HEIGHT);
            float l = (float) Convert.ToDouble(this.lumEdit.Text);
            this.hueEdit.Text = Convert.ToString((int) h);
            this.satEdit.Text = Convert.ToString((int) s);
            this.ConvertToRGB(h, s, l);
            this.updatingUI = false;
        }

        private void HueSatTextBoxChanged()
        {
            if ((this.hueEdit.Text != "") && !(this.satEdit.Text == ""))
            {
                this.updatingUI = true;
                float h = (float) Convert.ToDouble(this.hueEdit.Text);
                float s = (float) Convert.ToDouble(this.satEdit.Text);
                float l = (float) Convert.ToDouble(this.lumEdit.Text);
                this.CalculatePaletteMarkerCoordinates((int) h, (int) s);
                this.ConvertToRGB(h, s, l);
                this.updatingUI = false;
            }
        }

        private void InitializeComponent()
        {
            ResourceManager manager = new ResourceManager(typeof(CustomColorDlg));
            this.paletteBox = new PictureBox();
            this.lumBox = new PictureBox();
            this.currentColorPanel = new Panel();
            this.button1 = new Button();
            this.button2 = new Button();
            this.hueEdit = new NumericTextBox();
            this.satEdit = new NumericTextBox();
            this.lumEdit = new NumericTextBox();
            this.redEdit = new NumericTextBox();
            this.greenEdit = new NumericTextBox();
            this.blueEdit = new NumericTextBox();
            this.Label1 = new Label();
            this.label2 = new Label();
            this.label3 = new Label();
            this.label4 = new Label();
            this.label5 = new Label();
            this.label6 = new Label();
            this.label7 = new Label();
            base.SuspendLayout();
            this.paletteBox.BorderStyle = BorderStyle.FixedSingle;
            this.paletteBox.Location = new Point(10, 12);
            this.paletteBox.Name = "paletteBox";
            this.paletteBox.Size = new Size(0xca, 0xbc);
            this.paletteBox.TabIndex = 0;
            this.paletteBox.TabStop = false;
            this.paletteBox.Paint += new PaintEventHandler(this.paletteBox_Paint);
            this.paletteBox.MouseUp += new MouseEventHandler(this.paletteBox_MouseUp);
            this.paletteBox.MouseMove += new MouseEventHandler(this.paletteBox_MouseMove);
            this.paletteBox.MouseDown += new MouseEventHandler(this.paletteBox_MouseDown);
            this.lumBox.Location = new Point(220, 7);
            this.lumBox.Name = "lumBox";
            this.lumBox.Size = new Size(0x18, 0xc6);
            this.lumBox.TabIndex = 1;
            this.lumBox.TabStop = false;
            this.lumBox.Paint += new PaintEventHandler(this.LumBox_Paint);
            this.lumBox.MouseUp += new MouseEventHandler(this.lumBox_MouseUp);
            this.lumBox.MouseMove += new MouseEventHandler(this.lumBox_MouseMove);
            this.lumBox.MouseDown += new MouseEventHandler(this.lumBox_MouseDown);
            this.currentColorPanel.BackColor = SystemColors.Control;
            this.currentColorPanel.BorderStyle = BorderStyle.FixedSingle;
            this.currentColorPanel.Location = new Point(12, 0xd4);
            this.currentColorPanel.Name = "currentColorPanel";
            this.currentColorPanel.Size = new Size(0x42, 0x30);
            this.currentColorPanel.TabIndex = 2;
            this.button1.DialogResult = DialogResult.OK;
            this.button1.Location = new Point(0x34, 0x12a);
            this.button1.Name = "button1";
            this.button1.Size = new Size(0x40, 0x16);
            this.button1.TabIndex = 3;
            this.button1.Text = "Add Color";
            this.button2.DialogResult = DialogResult.Cancel;
            this.button2.Location = new Point(0x8a, 0x12a);
            this.button2.Name = "button2";
            this.button2.Size = new Size(0x40, 0x16);
            this.button2.TabIndex = 4;
            this.button2.Text = "Cancel";
            this.hueEdit.Location = new Point(0x7c, 0xda);
            this.hueEdit.MaxLength = 3;
            this.hueEdit.Name = "hueEdit";
            this.hueEdit.SetRange = new Size(0, 240);
            this.hueEdit.Size = new Size(30, 20);
            this.hueEdit.TabIndex = 5;
            this.hueEdit.Text = "160";
            this.hueEdit.TextChanged += new EventHandler(this.hueEdit_TextChanged);
            this.satEdit.Location = new Point(0x7c, 0xf1);
            this.satEdit.MaxLength = 3;
            this.satEdit.Name = "satEdit";
            this.satEdit.SetRange = new Size(0, 240);
            this.satEdit.Size = new Size(30, 20);
            this.satEdit.TabIndex = 6;
            this.satEdit.Text = "0";
            this.satEdit.TextChanged += new EventHandler(this.satEdit_TextChanged);
            this.lumEdit.Location = new Point(0x7c, 0x108);
            this.lumEdit.MaxLength = 3;
            this.lumEdit.Name = "lumEdit";
            this.lumEdit.SetRange = new Size(0, 240);
            this.lumEdit.Size = new Size(30, 20);
            this.lumEdit.TabIndex = 7;
            this.lumEdit.Text = "0";
            this.lumEdit.TextChanged += new EventHandler(this.lumEdit_TextChanged);
            this.redEdit.Location = new Point(200, 0xda);
            this.redEdit.MaxLength = 3;
            this.redEdit.Name = "redEdit";
            this.redEdit.SetRange = new Size(0, 0xff);
            this.redEdit.Size = new Size(30, 20);
            this.redEdit.TabIndex = 8;
            this.redEdit.Text = "0";
            this.redEdit.TextChanged += new EventHandler(this.redEdit_TextChanged);
            this.greenEdit.Location = new Point(200, 0xf1);
            this.greenEdit.MaxLength = 3;
            this.greenEdit.Name = "greenEdit";
            this.greenEdit.SetRange = new Size(0, 0xff);
            this.greenEdit.Size = new Size(30, 20);
            this.greenEdit.TabIndex = 9;
            this.greenEdit.Text = "0";
            this.greenEdit.TextChanged += new EventHandler(this.greenEdit_TextChanged);
            this.blueEdit.Location = new Point(200, 0x108);
            this.blueEdit.MaxLength = 3;
            this.blueEdit.Name = "blueEdit";
            this.blueEdit.SetRange = new Size(0, 0xff);
            this.blueEdit.Size = new Size(30, 20);
            this.blueEdit.TabIndex = 10;
            this.blueEdit.Text = "0";
            this.blueEdit.TextChanged += new EventHandler(this.blueEdit_TextChanged);
            this.Label1.Location = new Point(0x60, 220);
            this.Label1.Name = "Label1";
            this.Label1.Size = new Size(0x1a, 0x12);
            this.Label1.TabIndex = 11;
            this.Label1.Text = "Hue";
            this.Label1.TextAlign = ContentAlignment.MiddleCenter;
            this.label2.Location = new Point(0x60, 0xf3);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x1a, 0x12);
            this.label2.TabIndex = 12;
            this.label2.Text = "Sat";
            this.label2.TextAlign = ContentAlignment.MiddleCenter;
            this.label3.Location = new Point(0x60, 0x10a);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x1a, 0x12);
            this.label3.TabIndex = 13;
            this.label3.Text = "Lum";
            this.label3.TextAlign = ContentAlignment.MiddleCenter;
            this.label4.Location = new Point(0xa6, 220);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x22, 0x12);
            this.label4.TabIndex = 14;
            this.label4.Text = "Red:";
            this.label4.TextAlign = ContentAlignment.MiddleRight;
            this.label5.Location = new Point(0x9e, 0xf2);
            this.label5.Name = "label5";
            this.label5.Size = new Size(0x2a, 0x12);
            this.label5.TabIndex = 15;
            this.label5.Text = "Green:";
            this.label5.TextAlign = ContentAlignment.MiddleRight;
            this.label6.Location = new Point(0x9e, 0x10a);
            this.label6.Name = "label6";
            this.label6.Size = new Size(0x2a, 0x12);
            this.label6.TabIndex = 0x10;
            this.label6.Text = "Blue:";
            this.label6.TextAlign = ContentAlignment.MiddleRight;
            this.label7.Location = new Point(0x12, 0x10a);
            this.label7.Name = "label7";
            this.label7.Size = new Size(60, 14);
            this.label7.TabIndex = 0x11;
            this.label7.Text = "Color|Solid";
            this.AutoScaleBaseSize = new Size(5, 13);
            base.ClientSize = new Size(0xf6, 330);
            base.Controls.AddRange(new Control[] { 
                this.label7, this.label6, this.label5, this.label4, this.label3, this.label2, this.Label1, this.blueEdit, this.greenEdit, this.redEdit, this.lumEdit, this.satEdit, this.hueEdit, this.button2, this.button1, this.currentColorPanel, 
                this.lumBox, this.paletteBox
             });
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "CustomColorDlg";
            base.StartPosition = FormStartPosition.CenterParent;
            this.Text = "Define Color";
            base.Load += new EventHandler(this.CustomColorDlg_Load);
            base.ResumeLayout(false);
        }

        private void lumBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.lumBox.Capture = true;
            int y = e.Y;
            if (y < 5)
            {
                y = 5;
            }
            if (y > (this.lumBox.Height - 5))
            {
                y = this.lumBox.Height - 5;
            }
            this.lumEdit.Text = (240 - (((y - 5) * 240) / (this.lumBox.Height - 10))).ToString();
            this.LuminosityChanged();
        }

        private void lumBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.lumBox.Capture)
            {
                int y = e.Y;
                if (y < 5)
                {
                    y = 5;
                }
                if (y > (this.lumBox.Height - 5))
                {
                    y = this.lumBox.Height - 5;
                }
                this.lumEdit.Text = (240 - (((y - 5) * 240) / (this.lumBox.Height - 10))).ToString();
                this.LuminosityChanged();
            }
        }

        private void lumBox_MouseUp(object sender, MouseEventArgs e)
        {
            this.lumBox.Capture = false;
            this.LuminosityChanged();
        }

        private void LumBox_Paint(object sender, PaintEventArgs e)
        {
            Rectangle rect = new Rectangle(0, 5, this.lumBox.Width - 10, this.lumBox.Height - 10);
            LinearGradientBrush brush = new LinearGradientBrush(rect, Color.White, this.CurrentColor, LinearGradientMode.Vertical);
            ColorBlend blend = new ColorBlend(3);
            blend.Colors[0] = Color.White;
            blend.Colors[1] = this.paletteColor;
            blend.Colors[2] = Color.Black;
            blend.Positions[0] = 0f;
            blend.Positions[1] = 0.5f;
            blend.Positions[2] = 1f;
            brush.InterpolationColors = blend;
            e.Graphics.FillRectangle(brush, rect);
            brush.Dispose();
            Rectangle rectangle2 = rect;
            rectangle2.Inflate(0, 0);
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 1f), rectangle2);
            Point[] points = new Point[3];
            int y = 240 - Convert.ToInt32(this.lumEdit.Text);
            y = (((this.lumBox.Height - 10) * y) / 240) + 4;
            if (y > (this.lumBox.Height - 5))
            {
                y = this.lumBox.Height - 5;
            }
            if (y < 5)
            {
                y = 5;
            }
            points[0] = new Point(this.lumBox.Width - 8, y);
            points[1] = new Point(this.lumBox.Width - 2, y - 7);
            points[2] = new Point(this.lumBox.Width - 2, y + 7);
            e.Graphics.FillPolygon(Brushes.Black, points);
        }

        private void lumEdit_TextChanged(object sender, EventArgs e)
        {
            if (!this.updatingUI)
            {
                this.LuminosityChanged();
            }
        }

        private void LuminosityChanged()
        {
            if (this.lumEdit.Text.Length != 0)
            {
                this.updatingUI = true;
                float h = (float) Convert.ToDouble(this.hueEdit.Text);
                float s = (float) Convert.ToDouble(this.satEdit.Text);
                float l = (float) Convert.ToDouble(this.lumEdit.Text);
                this.ConvertToRGB(h, s, l);
                this.updatingUI = false;
            }
        }

        private void paletteBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.paletteBox.Capture = true;
            this.drawCrossHair = false;
            this.crossPos.X = e.X;
            this.crossPos.Y = e.Y;
            this.paletteBox.Invalidate();
        }

        private void paletteBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.paletteBox.Capture)
            {
                this.crossPos.X = e.X;
                this.crossPos.Y = e.Y;
                Rectangle clientRectangle = this.paletteBox.ClientRectangle;
                bool flag = false;
                if (e.X <= clientRectangle.Left)
                {
                    flag = true;
                    this.crossPos.X = clientRectangle.Left;
                }
                if (e.X >= clientRectangle.Right)
                {
                    flag = true;
                    this.crossPos.X = clientRectangle.Right;
                }
                if (e.Y <= clientRectangle.Top)
                {
                    flag = true;
                    this.crossPos.Y = clientRectangle.Top;
                }
                if (e.Y >= clientRectangle.Bottom)
                {
                    flag = true;
                    this.crossPos.Y = clientRectangle.Bottom;
                }
                if (flag)
                {
                    Cursor.Position = this.paletteBox.PointToScreen(new Point(this.crossPos.X, this.crossPos.Y));
                }
                this.HueSatChanged();
            }
        }

        private void paletteBox_MouseUp(object sender, MouseEventArgs e)
        {
            this.paletteBox.Capture = false;
            this.drawCrossHair = true;
            this.crossPos.X = e.X;
            this.crossPos.Y = e.Y;
            this.paletteBox.Invalidate();
            this.HueSatChanged();
        }

        private void paletteBox_Paint(object sender, PaintEventArgs e)
        {
            if (this.drawCrossHair)
            {
                Pen pen = new Pen(Brushes.Black, 3f);
                int x = this.crossPos.X;
                int y = this.crossPos.Y;
                e.Graphics.DrawLine(pen, x - 10, y, x - 5, y);
                e.Graphics.DrawLine(pen, x + 10, y, x + 5, y);
                e.Graphics.DrawLine(pen, x, y + 11, x, y + 5);
                e.Graphics.DrawLine(pen, x, y - 10, x, y - 4);
            }
        }

        private void redEdit_TextChanged(object sender, EventArgs e)
        {
            if (!this.updatingUI)
            {
                this.RGBTextChanged();
            }
        }

        private void RGBTextChanged()
        {
            if (((this.redEdit.Text != "") && (this.greenEdit.Text != "")) && !(this.blueEdit.Text == ""))
            {
                this.updatingUI = true;
                float r = (float) Convert.ToDouble(this.redEdit.Text);
                float g = (float) Convert.ToDouble(this.greenEdit.Text);
                float b = (float) Convert.ToDouble(this.blueEdit.Text);
                float h = 0f;
                float s = 0f;
                float l = 0f;
                ColorUtil.RGBToHSL((int) r, (int) g, (int) b, ref h, ref s, ref l);
                this.hueEdit.Text = Convert.ToString((int) h);
                this.satEdit.Text = Convert.ToString((int) s);
                this.lumEdit.Text = Convert.ToString((int) l);
                this.CurrentColor = Color.FromArgb((int) r, (int) g, (int) b);
                this.currentColorPanel.BackColor = this.CurrentColor;
                ColorUtil.HSLToRGB(h, s, 120f, ref r, ref g, ref b);
                this.paletteColor = Color.FromArgb((int) r, (int) g, (int) b);
                this.lumBox.Invalidate();
                this.CalculatePaletteMarkerCoordinates((int) h, (int) s);
                this.updatingUI = false;
            }
        }

        private void satEdit_TextChanged(object sender, EventArgs e)
        {
            if (!this.updatingUI)
            {
                this.HueSatTextBoxChanged();
            }
        }

        public Color CurrentColor
        {
            get
            {
                return this.currentColor;
            }
            set
            {
                this.currentColor = value;
            }
        }
    }
}

